import { create } from "domain";

const stepCache: number[] = [];
export function stepladder(n: number) {
    if (n === 1) {
        stepCache[n - 1] = 1;
        return 1;
    };
    if (n === 2) {
        stepCache[n - 1] = 2;
        return 2;
    }
    stepCache[n - 1] = stepladder(n - 1) + stepladder(n - 2);
    return stepCache[n - 1];
}

export function findCloseDis(map: number[][]) {
    const mapCache: number[][] = [];
    const x = map[0].length - 1;
    const y = map.length - 1;


    function findInstance(x: number, y: number) {
        if (!mapCache[y]) {
            mapCache[y] = [];
        }
        if (x === 0 && y === 0) {
            mapCache[0][0] = map[0][0];
        }
        if (x === 0 && y > 0) {
            mapCache[y][x] = findInstance(0, y - 1) + map[y][x];
        }
        if (x > 0 && y === 0) {
            mapCache[y][x] = findInstance(x - 1, 0) + map[y][x];
        }
        if (x > 0 && y > 0) {
            const xValue = findInstance(x - 1, y);
            const yValue = findInstance(x, y - 1);
            mapCache[y][x] = (xValue > yValue ? yValue : xValue) + map[y][x];
        }
        console.log('x', x, 'y', y, 'value', mapCache[y][x]);
        return mapCache[y][x];
    }
    return findInstance(x, y);
}

interface Node {
    value: number | string;
    left?: Node;
    right?: Node;
}
export function printFromTopToBottom(root: Node) {
    // 打印当前
    console.log(root.value);
    // 打印左边
    root.left && printFromTopToBottom(root.left);
    // 打印右边
    root.right && printFromTopToBottom(root.right);
}

export function createTree(arr: number[]) {
    function createNode(index: number, treeNode?: RenderTree) {
        const value = arr[index];
        let node: Node = {
            value: value
        };
        if (!treeNode) {
            treeNode = new RenderTree(value);
        }
        if (arr[index * 2 + 1] !== undefined) {
            treeNode.insertLeft(value);

            node.left = createNode(index * 2 + 1, treeNode.left)
        }

        if (arr[index * 2 + 2] !== undefined) {
            node.right = createNode(index * 2 + 2, treeNode.right)
            treeNode.insertRight(value);
        }
        return node;
    }
    const tree = new RenderTree(arr[1]);
    const treeNode = createNode(0, tree);
    return {
        tree,
        treeNode
    }
}

export class RenderTree {
    node: HTMLElement;

    value: number;

    right!: RenderTree;

    left!: RenderTree;

    constructor(value: number) {
        this.node = document.createElement('div');
        this.value = value;
        this.node.innerHTML = `
            <div class="text-content">${value}</div>
            <div className="child-content">
                <div id='left-${value}'></div>
                <div id='right-${value}'></div>
            </div>
        `

    }

    insertRight(value: number) {
        const tag = document.getElementById(`right-${this.value.toString()}`);
        const right = new RenderTree(value);
        this.right = right;
        tag?.parentElement?.insertBefore(right.node, tag);
    }

    insertLeft(value: number) {
        const tag = document.getElementById(`left-${this.value.toString()}`);
        const left = new RenderTree(value);
        this.left = left;
        tag?.parentElement?.insertBefore(left.node, tag);
    }
}

export function topToBottom(tree: Node) {
    function print(current: Node[], child?: Node[]) {
        current.forEach(node => console.log(node.value));
        if (!child) {
            child = current.flatMap(node => [node.left, node.right]).filter(Boolean) as Node[];
        }
        let grandChild: Node[] = child.flatMap(node => [node.left, node.right]).filter(Boolean) as Node[];
        print(child, grandChild);
    }
    print([tree]);
}